<?php

Form::macro('labelGroup', function($name, $display_name, $default = NULL, $errors = null, $tab_index = 1, $is_required = false, $size = 4)
{
    $item = '<!-- '.$name.' -->';

    switch($size){
        case 3:
            $div_size = 'col-lg-3 col-md-4 col-xs-12';
            break;
        case 4:
            $div_size = 'col-lg-4 col-md-6 col-xs-12';
            break;
        case 6:
            $div_size = 'col-lg-6 col-md-12 col-xs-12';
            break;
        case 12:
            $div_size = 'col-lg-12 col-md-12 col-xs-12';
            break;
    }

    $item .= '<div class="form-group row '.($errors->has($name) ? 'has-error' : '').' '.$div_size.'">';

    if($is_required){
        $item .= Form::label($name, '*', array('style'=>'color: #ff0000; margin-left: 20px;'));
    }

    $item .= Form::label($name, $display_name, $is_required ? array() : array('style'=>'margin-left: 20px;'));
    $item .= '<div class="col-lg-12 col-md-12 col-xs-12">';
    $item .= Form::label($name, $default);
    $item .= Form::hidden($name, $default);
    $item .= $errors->first($name, '<label class="control-label" for="'.$name.'"><i class="fa fa-times-circle-o"></i> :message</label>');
    $item .= '</div>';
    $item .= '</div>';
    $item .= '<!-- ./ '.$name.' -->';

    return $item;
});

Form::macro('textGroup', function($name, $display_name, $default = NULL, $errors = null, $tab_index = 1, $is_required = false, $size = 4)
{
    $item = '<!-- '.$name.' -->';

    switch($size){
        case 3:
            $div_size = 'col-lg-3 col-md-4 col-xs-12';
            break;
        case 4:
            $div_size = 'col-lg-4 col-md-6 col-xs-12';
            break;
        case 6:
            $div_size = 'col-lg-6 col-md-12 col-xs-12';
            break;
        case 12:
            $div_size = 'col-lg-12 col-md-12 col-xs-12';
            break;
    }

    $item .= '<div class="form-group row '.($errors->has($name) ? 'has-error' : '').' '.$div_size.'">';

    if($is_required){
        $item .= Form::label($name, '*', array('style'=>'color: #ff0000; margin-left: 20px;'));
    }

    $item .= Form::label($name, $display_name, $is_required ? array() : array('style'=>'margin-left: 20px;'));
    $item .= '<div class="col-lg-12 col-md-12 col-xs-12">';
    $item .= Form::text($name, $default, array('class'=>'form-control','tabindex'=>$tab_index,'placeholder'=>$display_name));
    $item .= $errors->first($name, '<label class="control-label" for="'.$name.'"><i class="fa fa-times-circle-o"></i> :message</label>');
    $item .= '</div>';
    $item .= '</div>';
    $item .= '<!-- ./ '.$name.' -->';

    return $item;
});

Form::macro('passwordGroup', function($name, $display_name, $errors = null, $tab_index = 1, $is_required = false, $size = 4)
{
    $item = '<!-- '.$name.' -->';

    switch($size){
        case 3:
            $div_size = 'col-lg-3 col-md-4 col-xs-12';
            break;
        case 4:
            $div_size = 'col-lg-4 col-md-6 col-xs-12';
            break;
        case 6:
            $div_size = 'col-lg-6 col-md-12 col-xs-12';
            break;
        case 12:
            $div_size = 'col-lg-12 col-md-12 col-xs-12';
            break;
    }

    $item .= '<div class="form-group row '.($errors->has($name) ? 'has-error' : '').' '.$div_size.'">';

    if($is_required){
        $item .= Form::label($name, '*', array('style'=>'color: #ff0000; margin-left: 20px;'));
    }

    $item .= Form::label($name, $display_name, $is_required ? array() : array('style'=>'margin-left: 20px;'));
    $item .= '<div class="col-lg-12 col-md-12 col-xs-12">';
    $item .= Form::password($name, array('class'=>'form-control','tabindex'=>$tab_index,'placeholder'=>$display_name));
    $item .= $errors->first($name, '<label class="control-label" for="'.$name.'"><i class="fa fa-times-circle-o"></i> :message</label>');
    $item .= '</div>';
    $item .= '</div>';
    $item .= '<!-- ./ '.$name.' -->';

    return $item;
});

Form::macro('selectGroup', function($name, $display_name, $list = array(), $default = NULL, $errors = null, $tab_index = 1, $is_required = false, $size = 4)
{
    $item = '<!-- '.$name.' -->';

    switch($size){
        case 3:
            $div_size = 'col-lg-3 col-md-4 col-xs-12';
            break;
        case 4:
            $div_size = 'col-lg-4 col-md-6 col-xs-12';
            break;
        case 6:
            $div_size = 'col-lg-6 col-md-12 col-xs-12';
            break;
        case 12:
            $div_size = 'col-lg-12 col-md-12 col-xs-12';
            break;
    }

    $item .= '<div class="form-group row '.($errors->has($name) ? 'has-error' : '').' '.$div_size.'">';
    //is required
    if($is_required){
        $item .= Form::label($name, '*', array('style'=>'color: #ff0000; margin-left: 20px;'));
    }

    $item .= Form::label($name, $display_name, $is_required ? array() : array('style'=>'margin-left: 20px;'));
    $item .= '<div class="col-lg-12 col-md-12 col-xs-12">';
    $item .= Form::select($name, $list, $default, array('class'=>'form-control','tabindex'=>$tab_index));
    $item .= $errors->first($name, '<label class="control-label" for="'.$name.'"><i class="fa fa-times-circle-o"></i> :message</label>');
    $item .= '</div>';
    $item .= '</div>';
    $item .= '<!-- ./ '.$name.' -->';

    return $item;
});

Form::macro('datetimeGroup', function($name, $display_name, $default, $errors = null, $tab_index = 1, $is_required = false, $size = 4)
{
    $item = '<!-- '.$name.' -->';

    switch($size){
        case 3:
            $div_size = 'col-lg-3 col-md-4 col-xs-12';
            break;
        case 4:
            $div_size = 'col-lg-4 col-md-6 col-xs-12';
            break;
        case 6:
            $div_size = 'col-lg-6 col-md-12 col-xs-12';
            break;
        case 12:
            $div_size = 'col-lg-12 col-md-12 col-xs-12';
            break;
    }

    $item .= '<div class="form-group row '.($errors->has($name) ? 'has-error' : '').' '.$div_size.'">';
    //is required
    if($is_required){
        $item .= Form::label($name, '*', array('style'=>'color: #ff0000; margin-left: 20px;'));
    }

    $item .= Form::label($name, $display_name, $is_required ? array() : array('style'=>'margin-left: 20px;'));
    $item .= '<div class="col-lg-12 col-md-12 col-xs-12">';
    $item .= Form::text($name, $default, array('class'=>'form-control','tabindex'=>$tab_index,'data-inputmask'=>"'alias': 'yyyy-mm-dd'", 'style'=>'margin:0px;', 'data-mask'));
    $item .= $errors->first($name, '<label class="control-label" for="'.$name.'"><i class="fa fa-times-circle-o"></i> :message</label>');
    $item .= '</div>';
    $item .= '</div>';
    $item .= '<!-- ./ '.$name.' -->';

    return $item;
});

Form::macro('checkboxGroup', function($name, $display_name, $list = array(), $errors = null, $tab_index = 1, $is_required = false, $size = 4)
{
    $item = '<!-- '.$name.' -->';

    switch($size){
        case 3:
            $div_size = 'col-lg-3 col-md-4 col-xs-12';
            break;
        case 4:
            $div_size = 'col-lg-4 col-md-6 col-xs-12';
            break;
        case 6:
            $div_size = 'col-lg-6 col-md-12 col-xs-12';
            break;
        case 12:
            $div_size = 'col-lg-12 col-md-12 col-xs-12';
            break;
    }

    $item .= '<div class="form-group row '.($errors->has($name) ? 'has-error' : '').' '.$div_size.'">';
    //is required
    if($is_required){
        $item .= Form::label($name, '*', array('style'=>'color: #ff0000; margin-left: 20px;'));
    }

    $item .= Form::label($name, $display_name, $is_required ? array() : array('style'=>'margin-left: 20px;'));
    $item .= '<div class="col-lg-12 col-md-12 col-xs-12">';

    foreach ($list as $value){
        $item .= '<label>';
        $item .= Form::checkbox($name.'['.$value['id'].']', $value['id'], $value['checked'], array('class'=>'form-control','tabindex'=>$tab_index++));
        $item .= $value['display_name'].'&nbsp;&nbsp;&nbsp;&nbsp;';
        $item .= '</label>';
    }
    $item .= $errors->first($name, '<label class="control-label" for="'.$name.'"><i class="fa fa-times-circle-o"></i> :message</label>');
    $item .= '</div>';
    $item .= '</div>';
    $item .= '<!-- ./ '.$name.' -->';

    return $item;
});

Form::macro('textAreaGroup', function($name, $display_name, $default = NULL, $errors = null, $tab_index = 1, $is_required = false, $size = 4)
{
    $item = '<!-- '.$name.' -->';

    switch($size){
        case 3:
            $div_size = 'col-lg-3 col-md-4 col-xs-12';
            break;
        case 4:
            $div_size = 'col-lg-4 col-md-6 col-xs-12';
            break;
        case 6:
            $div_size = 'col-lg-6 col-md-12 col-xs-12';
            break;
        case 12:
            $div_size = 'col-lg-12 col-md-12 col-xs-12';
            break;
    }

    $item .= '<div class="form-group row '.($errors->has($name) ? 'has-error' : '').' '.$div_size.'">';

    if($is_required){
        $item .= Form::label($name, '*', array('style'=>'color: #ff0000; margin-left: 20px;'));

    }

    $item .= Form::label($name, $display_name, $is_required ? array() : array('style'=>'margin-left: 20px;'));
    $item .= '<div class="col-lg-12 col-md-12 col-xs-12">';
    $item .= Form::textarea($name, $default, array('class'=>'form-control ckeditor','tabindex'=>$tab_index,'placeholder'=>$display_name, 'cols'=>'80', 'rows'=>'10'));
    $item .= $errors->first($name, '<label class="control-label" for="'.$name.'"><i class="fa fa-times-circle-o"></i> :message</label>');
    $item .= '</div>';
    $item .= '</div>';
    $item .= '<!-- ./ '.$name.' -->';

    return $item;
});

Form::macro('thumbGroup', function($name, $display_name, $thumbs = array(), $errors = null, $tab_index = 1, $is_required = false, $size = 4)
{
    $item = '<!-- '.$name.' -->';

    switch($size){
        case 3:
            $div_size = 'col-lg-3 col-md-4 col-xs-12';
            break;
        case 4:
            $div_size = 'col-lg-4 col-md-6 col-xs-12';
            break;
        case 6:
            $div_size = 'col-lg-6 col-md-12 col-xs-12';
            break;
        case 12:
            $div_size = 'col-lg-12 col-md-12 col-xs-12';
            break;
    }

    $item .= '<div class="form-group row '.($errors->has($name) ? 'has-error' : '').' '.$div_size.'">';

    if($is_required){
        $item .= Form::label($name, '*', array('style'=>'color: #ff0000; margin-left: 20px;'));
    }

    $item .= Form::label($name, $display_name, $is_required ? array() : array('style'=>'margin-left: 20px;'));
    $item .= '<div class="col-lg-12 col-md-12 col-xs-12">';
    $item .= '<input id="imageupload" type="file" name="upload[]" multiple tabindex="'.$tab_index.'" />';
    $item .= '<div class="progress progress-striped active"><div class="bar" style="width: 0%;"></div></div>';
    $item .= '<div class="thumbnails-box row">';

    if($thumbs instanceof Illuminate\Database\Eloquent\Collection){
        foreach($thumbs as $attach){
            $item .= '<div class="thumbnails">';
            $item .= '<button type="button" class="btn btn-danger destroy" data-file="'. $attach->path .'"><i class="icon-trash"></i><span>Delete</span></button>';
            $item .= '<a href="#" class="thumbnail"><img alt="" src="'. URL::to('file/image_crop/4/3/0?name=') . $attach->path .'"></a>';
            $item .= '</div>';
            $item .= '<input class="form-control" type="hidden" name="'.$name.'['.$attach->path .']" id="'.$name.'['. $attach->path .']" value="'. $attach->path .'" />';
        }
    }
    $item .= '</div>';
    $item .= $errors->first($name, '<label class="control-label" for="'.$name.'"><i class="fa fa-times-circle-o"></i> :message</label>');
    $item .= '</div>';
    $item .= '</div>';
    $item .= '<!-- ./ '.$name.' -->';

    return $item;
});

Form::macro('submitButtonGroup', function($return_action, $can_submit, $tab_index = 1, $size = 12)
{
    $item = '<!-- Form Actions -->';

    switch($size){
        case 3:
            $div_size = 'col-lg-3 col-md-4 col-xs-12 col-lg-offset-1 col-md-offset-1 col-xs-offset-4';
            break;
        case 4:
            $div_size = 'col-lg-4 col-md-6 col-xs-12 col-lg-offset-1 col-md-offset-2 col-xs-offset-4';
            break;
        case 6:
            $div_size = 'col-lg-6 col-md-12 col-xs-12 col-lg-offset-2 col-md-offset-4 col-xs-offset-4';
            break;
        case 12:
            $div_size = 'col-lg-12 col-md-12 col-xs-12 col-lg-offset-4 col-md-offset-4 col-xs-offset-4';
            break;
    }

    $item .= '<div class="form-group row">';
    $item .= '<div class="'.$div_size.'">';

    $item .= link_to_action($return_action, Lang::get('button.return'), array(), array('type'=>'reset', 'class'=>'btn btn-default btn-lg', 'tabindex'=>$tab_index++));
    if($can_submit){
        $item .= Form::submit(Lang::get('button.submit'), array('name'=>'submit', 'class'=>'btn btn-success btn-lg col-lg-offset-1 col-md-offset-1 col-xs-offset-1','tabindex'=>$tab_index));
    }
    $item .= '</div>';
    $item .= '</div>';
    $item .= '<!-- ./ form actions -->';

    return $item;
});


Form::macro('textBetweenGroup', function($name, $display_name, $default_start = NULL, $default_end = NULL, $errors = null, $tab_index = 1, $is_required = false, $size = 4)
{
    $item = '<!-- '.$name.' -->';

    switch($size){
        case 3:
            $div_size = 'col-lg-3 col-md-4 col-xs-12';
            break;
        case 4:
            $div_size = 'col-lg-4 col-md-6 col-xs-12';
            break;
        case 6:
            $div_size = 'col-lg-6 col-md-12 col-xs-12';
            break;
        case 12:
            $div_size = 'col-lg-12 col-md-12 col-xs-12';
            break;
    }

    $item .= '<div class="form-group row '.($errors->has($name) ? 'has-error' : '').' '.$div_size.'">';

    if($is_required){
        $item .= Form::label($name, '*', array('style'=>'color: #ff0000; margin-left: 20px;'));
    }

    $item .= Form::label($name, $display_name, $is_required ? array() : array('style'=>'margin-left: 20px;'));
    $item .= '<div class="col-lg-12 col-md-12 col-xs-12">';
    $item .= Form::text($name.'_start', $default_start, array('class'=>'form-control col-lg-5 col-md-5 col-xs-5', 'style'=>' width: 45%; ', 'tabindex'=>$tab_index,'placeholder'=>$display_name));
    $item .= Form::label($name, '从', array('class'=>'col-lg-1 col-md-1 col-xs-1', 'style'=>'margin-left: -10px;margin-top: 7px;'));
    $item .= Form::text($name.'_end', $default_end, array('class'=>'form-control col-lg-5 col-md-5 col-xs-5', 'style'=>' width: 45%; ', 'tabindex'=>$tab_index,'placeholder'=>$display_name));
    $item .= $errors->first($name, '<label class="control-label" for="'.$name.'"><i class="fa fa-times-circle-o"></i> :message</label>');
    $item .= '</div>';
    $item .= '</div>';
    $item .= '<!-- ./ '.$name.' -->';

    return $item;
});


Form::macro('datetimeBetweenGroup', function($name, $display_name, $default_start = NULL, $default_end = NULL, $errors = null, $tab_index = 1, $is_required = false, $size = 4)
{
    $item = '<!-- '.$name.' -->';

    switch($size){
        case 3:
            $div_size = 'col-lg-3 col-md-4 col-xs-12';
            break;
        case 4:
            $div_size = 'col-lg-4 col-md-6 col-xs-12';
            break;
        case 6:
            $div_size = 'col-lg-6 col-md-12 col-xs-12';
            break;
        case 12:
            $div_size = 'col-lg-12 col-md-12 col-xs-12';
            break;
    }

    $item .= '<div class="form-group row '.($errors->has($name) ? 'has-error' : '').' '.$div_size.'">';

    if($is_required){
        $item .= Form::label($name, '*', array('style'=>'color: #ff0000; margin-left: 20px;'));
    }

    $item .= Form::label($name, $display_name, $is_required ? array() : array('style'=>'margin-left: 20px;'));
    $item .= '<div class="col-lg-12 col-md-12 col-xs-12">';
    $item .= Form::text($name.'_start', $default_start, array('class'=>'form-control col-lg-5 col-md-5 col-xs-5', 'style'=>' width: 45%;margin:0px; ', 'tabindex'=>$tab_index,'placeholder'=>$display_name, 'data-inputmask'=>"'alias': 'yyyy-mm-dd'", 'data-mask'));
    $item .= Form::label($name, '从', array('class'=>'col-lg-1 col-md-1 col-xs-1', 'style'=>'margin-left: -10px;margin-top: 7px;'));
    $item .= Form::text($name.'_end', $default_end, array('class'=>'form-control col-lg-5 col-md-5 col-xs-5', 'style'=>' width: 45%;margin:0px; ', 'tabindex'=>$tab_index,'placeholder'=>$display_name, 'data-inputmask'=>"'alias': 'yyyy-mm-dd'", 'data-mask'));
    $item .= $errors->first($name, '<label class="control-label" for="'.$name.'"><i class="fa fa-times-circle-o"></i> :message</label>');
    $item .= '</div>';
    $item .= '</div>';
    $item .= '<!-- ./ '.$name.' -->';

    return $item;
});

Form::macro('orderLink', function($links, $display_name, $field_name, $is_number_icon = false)
{
    //排序图标数字显示还是字母显示
    $icon = 'alpha';
    if($is_number_icon){
        $icon = 'numeric';
    }
    $item = '<a href="?'.String::buildQuery(String::setQueryValue($links,'reverse', Input::get( 'reverse') == 1 ? 0 : 1),'order',$field_name) .'" >';
    $item .= (Input::get( 'order') == $field_name) ? '<i class="fa '.((Input::get( 'reverse') == 1)?'fa-sort-'.$icon.'-asc':'fa-sort-'.$icon.'-desc' ) . '"></i>' : '';
    $item .= $display_name.'</a>';

    return $item;
});



?>
